/*
 * @Author: Laputa
 * @Version: V0.1
 * @Date: 2023-06-12 10:48:01
 * @LastEditors: Laputa
 * @LastEditTime: 2023-10-13 15:05:31
 * @Description: This file contains the functions prototypes for the PIT
 * firmware library.
 *
 * Copyright (c) 2023 by Levetop, All Rights Reserved.
 */

#ifndef _LT168_PIT_H
#define _LT168_PIT_H

#include "LT168.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/* PIT PMR */
#define PIT_PMR_PM_MASK                               (0xFFFFU)
#define PIT_PMR_PM_SHIFT                              (0U)
#define PIT_PMR_PM(x)                                 (((uint32_t)(((uint32_t)(x)) << PIT_PMR_PM_SHIFT)) & PIT_PMR_PM_MASK)

/* PIT PCSR */
#define PIT_PCSR_EN_MASK                              (0x1U)
#define PIT_PCSR_EN_SHIFT                             (0U)
#define PIT_PCSR_EN(x)                                (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_EN_SHIFT)) & PIT_PCSR_EN_MASK)

#define PIT_PCSR_RLD_MASK                             (0x2U)
#define PIT_PCSR_RLD_SHIFT                            (1U)
#define PIT_PCSR_RLD(x)                               (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_RLD_SHIFT)) & PIT_PCSR_RLD_MASK)

#define PIT_PCSR_PIF_MASK                             (0x4U)
#define PIT_PCSR_PIF_SHIFT                            (2U)
#define PIT_PCSR_PIF(x)                               (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_PIF_SHIFT)) & PIT_PCSR_PIF_MASK)

#define PIT_PCSR_PIE_MASK                             (0x8U)
#define PIT_PCSR_PIE_SHIFT                            (3U)
#define PIT_PCSR_PIE(x)                               (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_PIE_SHIFT)) & PIT_PCSR_PIE_MASK)

#define PIT_PCSR_OVW_MASK                             (0x10U)
#define PIT_PCSR_OVW_SHIFT                            (4U)
#define PIT_PCSR_OVW(x)                               (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_OVW_SHIFT)) & PIT_PCSR_OVW_MASK)

#define PIT_PCSR_PDBG_MASK                            (0x20U)
#define PIT_PCSR_PDBG_SHIFT                           (5U)
#define PIT_PCSR_PDBG(x)                              (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_PDBG_SHIFT)) & PIT_PCSR_PDBG_MASK)

#define PIT_PCSR_PDOZE_MASK                           (0x40U)
#define PIT_PCSR_PDOZE_SHIFT                          (6U)
#define PIT_PCSR_PDOZE(x)                             (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_PDOZE_SHIFT)) & PIT_PCSR_PDOZE_MASK)

#define PIT_PCSR_PDMAE_MASK                           (0x80U)
#define PIT_PCSR_PDMAE_SHIFT                          (7U)
#define PIT_PCSR_PDMAE(x)                             (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_PDMAE_SHIFT)) & PIT_PCSR_PDMAE_MASK)

#define PIT_PCSR_PRE_MASK                             (0xF00U)
#define PIT_PCSR_PRE_SHIFT                            (8U)
#define PIT_PCSR_PRE(x)                               (((uint32_t)(((uint32_t)(x)) << PIT_PCSR_PRE_SHIFT)) & PIT_PCSR_PRE_MASK)

/* PIT PCNTR */
#define PIT_PCNTR_PC_MASK                             (0xFFFFU)
#define PIT_PCNTR_PC_SHIFT                            (0U)
#define PIT_PCNTR_PC(x)                               (((uint32_t)(((uint32_t)(x)) << PIT_PCNTR_PC_SHIFT)) & PIT_PCNTR_PC_MASK)

/**
 * @brief: PIT Prescaler Enumeration
 */
typedef enum
{
  PIT_Pre_1     = 0,
  PIT_Pre_2     = 1,
  PIT_Pre_4     = 2,
  PIT_Pre_8     = 3,
  PIT_Pre_16    = 4,
  PIT_Pre_32    = 5,
  PIT_Pre_64    = 6,
  PIT_Pre_128   = 7,
  PIT_Pre_256   = 8,
  PIT_Pre_512   = 9,
  PIT_Pre_1024  = 10,
  PIT_Pre_2048  = 11,
  PIT_Pre_4096  = 12,
  PIT_Pre_8192  = 13,
  PIT_Pre_16384 = 14,
  PIT_Pre_32768 = 15
} pit_prescaler_te;

/**
 * @brief: PIT Reload Enumeration
 */
typedef enum
{
  PIT_Rld_0xFFFF = 0,
  PIT_Rld_PMR    = 1
} pit_reload_te;

/**
 * @brief: PIT Overwrite Enumeration
 */
typedef enum
{
  PIT_Ovm_0x0000         = 0,
  PIT_Ovm_Immediately    = 1
} pit_overwrite_te;

/**
 * @brief: PIT Initialization Structure Definition
 */
typedef struct
{
    uint16_t pmr;
    pit_prescaler_te prescaler;

    struct
    {
        FunctionState en : 1;     // PIT Enable
        pit_reload_te rld : 1;    // Reload
        ITStatus pif : 1;         // Interrupt Flag
        FunctionState pie : 1;    // Interrupt Enable
        pit_overwrite_te ovm : 1; // Overwrite
        uint8_t pdbg : 1;         // Don't care
        uint8_t pdoze : 1;        // Don't care
        FunctionState pdmae : 1;  // DMA Enable

    } pit_cs;
} pit_config_ts;

/*!<---------------End of Definitions--------------->!*/


/*******************************************************************************
 * APIs
 ******************************************************************************/


// PIT Initialization Functions
void PIT_GetDefault(pit_config_ts *pit_config);
void PIT_Init(pit *pitx, pit_config_ts *pit_config);

// PIT Operating Function
void PIT_Write_PMR(pit *pitx, uint16_t pmr);
uint16_t PIT_Get_Count_Value(pit *pitx);

// PIT Command Functions
void PIT_Enable(pit *pitx);
void PIT_Disable(pit *pitx);

// PIT Interrupt Function
void PIT_Clear_IntFlag(pit *pitx);

/*!<---------------End of APIs--------------->!*/
#endif
/*!<---------------End of _LT168_PIT_H --------------->!*/